﻿
/*----------------------------------------------------------------
 * 创建时间：2023/7/27 16:49:56
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Core.Mvvm 
 * 描  述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using Minotaur.App.Core.Contrls;
using Minotaur.App.Core.Redis;
using Minotaur.App.Domains.ViewModel;
using Prism.Commands;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Minotaur.App.Core.Mvvm
{
    public abstract class WindowComputeViewModel : RegionViewModelBase
    {
        protected WindowComputeViewModel(IRegionManager regionManager, IDbContext dbContext, IRedisClient _redisClient) : base(regionManager, dbContext, _redisClient)
        {

        }

        private bool _OpendLoading;
        /// <summary>
        /// 打开Loading
        /// </summary>
        public bool OpendLoading
        {
            get => _OpendLoading;
            set
            {
                _OpendLoading = value;
                if (!value)
                {
                    AppDataContext.GlobalLoadingView.IsRunning = false;
                    Thread.Sleep(300);
                    AppDataContext.GlobalLoadingView.Visibility = Visibility.Collapsed;
                    AppDataContext.GlobalLoadingGrid.Visibility = Visibility.Collapsed;
                }
                else
                {
                    AppDataContext.GlobalLoadingView.IsRunning = true;
                    AppDataContext.GlobalLoadingGrid.Visibility = Visibility.Visible;
                    AppDataContext.GlobalLoadingView.Visibility = Visibility.Visible;
                }
            }
        }
        private string _MessageQueue;

        // <summary>  
        /// 获得指定元素的所有子元素  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="obj"></param>  
        /// <returns></returns>  

        /// <summary>
        /// 初始化表单按钮
        /// </summary>
        public virtual void InitBtnEnabel()
        {
            List<TextBox> textBoxes = CoordinateUilts.GetChildObjects<TextBox>(this.FormDependencyObject).Where(o => (o.Tag + "") == TagConst.ValidateionTag).ToList();
            TextBoxes = textBoxes;
            foreach (var item in textBoxes)
            {
                item.TextChanged += TextChanged;
            }
            this.SubmitFormButton.IsEnabled = !this.TextBoxes.Any(o => string.IsNullOrEmpty(o.Text));
        }

        /// <summary>
        /// 文本框输入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            ResetSubmitButton();
        }

        /// <summary>
        /// 重置按钮
        /// </summary>
        private void ResetSubmitButton()
        {
            bool b = true;
            foreach (var item in this.TextBoxes)
            {
                if (System.Windows.Controls.Validation.GetHasError(item) || string.IsNullOrEmpty(item.Text))
                {
                    b = false;
                    break;
                }
            }
            this.SubmitFormButton.IsEnabled = b;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        public abstract void InitData();

        #region 属性注册区域


        /// <summary>
        /// 提交表单事项
        /// </summary>
        public Button SubmitFormButton { get; set; }

        /// <summary>
        /// 表单所在的父级菜单中
        /// </summary>
        public DependencyObject FormDependencyObject { get; set; }

        /// <summary>
        /// 页面所在的元素
        /// </summary>
        public DependencyObject BodyDependencyObject { get; set; }

        /// <summary>
        /// 表单中的输入框
        /// </summary>
        public List<TextBox> TextBoxes { get; set; } = new List<TextBox>();
        /// <summary>
        /// 右侧菜单
        /// </summary>
        private bool _IsRightDrawerOpen;

        /// <summary>
        /// 打开右边菜单栏
        /// </summary>
        public bool IsRightDrawerOpen
        {
            get => _IsRightDrawerOpen;
            set => SetProperty(ref _IsRightDrawerOpen, value);
        }

        /// <summary>
        /// 左侧菜单
        /// </summary>
        private bool _IsLeftDrawerOpen;

        /// <summary>
        /// 打开左边菜单栏
        /// </summary>
        public bool IsLeftDrawerOpen
        {
            get => _IsLeftDrawerOpen;
            set => SetProperty(ref _IsLeftDrawerOpen, value);
        }

        /// <summary>
        /// 分页控件
        /// </summary>
        public DependencyObject ListPagination { get; set; }

        /// <summary>
        /// 查询条件
        /// </summary>
        private SearchViewModel searchViewModel = new SearchViewModel();
        public SearchViewModel SearchViewModel { get => searchViewModel; set => SetProperty(ref searchViewModel, value); }
        /// <summary>
        /// 成功提示
        /// </summary>
        public string ShowSuccess
        {
            get => _ShowSuccess;
            set
            {
                _ShowSuccess = value;
                if (!string.IsNullOrEmpty(value))
                {
                    Task.Factory.StartNew(() => HandyControl.Controls.Growl.Success(value));
                    Thread.Sleep(500);
                }
            }
        }

        private string _ShowSuccess;
        #endregion
        #region 命令注册

        #endregion
    }
}
